package cn.xmoit.flowable.service;

import cn.xmoit.flowable.mapper.UserMapper;
import cn.xmoit.flowable.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fangyy
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	UserMapper userMapper;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = userMapper.loadUserByUsername(username);
		if (user != null) {
			user.setRoles(userMapper.getUserRolesByUserId(user.getId()));
		}
		return user;
	}

	public List<User> getAllUsers() {
		return userMapper.getAllUsers();
	}

}
